﻿
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OA.UserManager
{
    /// <summary>
    /// 具体操作，例如：添加、删除和查询等
    /// </summary>
    public class Action
    {
        #region 属性
        public string Name { get; set; }
        public int Id { get; set; }
        #endregion
        #region 方法
        /// <summary>
        /// 通过标识符获取对象实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Action GetActionById(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Name from Actions ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            Action model = new Action();
            DataSet ds = DbHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 分页获取所有操作
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="size">每页记录数</param>
        /// <returns></returns>
        public static QueryResult<Action> GetAllActions(int page, int size)
        {
            QueryResult<Action> result = new QueryResult<Action>(page, size);
            #region 设置存储过程参数
            SqlParameter[] parameters = {
               new SqlParameter("@tblName",SqlDbType.NVarChar,200),
               new SqlParameter("@fldName",SqlDbType.NVarChar,500),
               new SqlParameter("@pageSize",SqlDbType.Int,4),
               new SqlParameter("@page",SqlDbType.Int,4),
               new SqlParameter("@pageCount",SqlDbType.Int,4),
               new SqlParameter("@Counts",SqlDbType.Int,4),
               new SqlParameter("@fldSort",SqlDbType.NVarChar,200),
               new SqlParameter("@Sort",SqlDbType.Bit,1),
               new SqlParameter("@strCondition",SqlDbType.NVarChar,1000),
               new SqlParameter("@ID",SqlDbType.NVarChar,150),
               new SqlParameter("@Dist",SqlDbType.Bit,1)
            };
            parameters[0].Value = "Actions";
            parameters[1].Value = "Id,Name";
            parameters[2].Value = size;
            parameters[3].Value = page;
            parameters[4].Direction = ParameterDirection.Output;
            parameters[5].Direction = ParameterDirection.Output;
            parameters[6].Value = "CreateOn";
            parameters[7].Value = true;
            parameters[8].Value = "";
            parameters[9].Value = "Id";
            parameters[10].Value = false;
            #endregion
            SqlDataReader reader = DbHelper.RunProcedure("proc_QueryPage", parameters);
            Action action = new Action();
            while (reader.Read())
            {
                action.Id = reader.GetInt32(0);
                action.Name = reader.GetString(1);
                result.DataList.Add(action);
            }
            result.Count = int.Parse(parameters[5].Value.ToString());
            result.PageCount = int.Parse(parameters[4].Value.ToString());
            return result;
        }
        public static QueryResult<Action> GetActionsByFeatureModule(FeatureModule featureModule, int page, int size)
        {
            QueryResult<Action> result = new QueryResult<Action>(page, size);
            #region 设置存储过程参数
            SqlParameter[] parameters = {
               new SqlParameter("@tblName",SqlDbType.NVarChar,200),
               new SqlParameter("@fldName",SqlDbType.NVarChar,500),
               new SqlParameter("@pageSize",SqlDbType.Int,4),
               new SqlParameter("@page",SqlDbType.Int,4),
               new SqlParameter("@pageCount",SqlDbType.Int,4),
               new SqlParameter("@Counts",SqlDbType.Int,4),
               new SqlParameter("@fldSort",SqlDbType.NVarChar,200),
               new SqlParameter("@Sort",SqlDbType.Bit,1),
               new SqlParameter("@strCondition",SqlDbType.NVarChar,1000),
               new SqlParameter("@ID",SqlDbType.NVarChar,150),
               new SqlParameter("@Dist",SqlDbType.Bit,1)
            };
            parameters[0].Value = "Features INNER Actions on Features.ActionId=Actions.Id";
            parameters[1].Value = "Actions.Id,Actions.Name";
            parameters[2].Value = size;
            parameters[3].Value = page;
            parameters[4].Direction = ParameterDirection.Output;
            parameters[5].Direction = ParameterDirection.Output;
            parameters[6].Value = "Actions.Id";
            parameters[7].Value = true;
            parameters[8].Value = "";
            parameters[9].Value = "Id";
            parameters[10].Value = false;
            #endregion
            SqlDataReader reader = DbHelper.RunProcedure("proc_QueryPage", parameters);
            Action action = new Action();
            while (reader.Read())
            {
                action.Id = reader.GetInt32(0);
                action.Name = reader.GetString(1);
                result.DataList.Add(action);
            }
            result.Count = int.Parse(parameters[5].Value.ToString());
            result.PageCount = int.Parse(parameters[4].Value.ToString());
            return result;
        }

        #endregion
    }
}
